package com.yin.verify;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import com.google.common.collect.Lists;

public class Util {

	public static String getHWID() {
		try {
			StringBuilder sb = new StringBuilder();
			String main = System.getenv("PROCESS_IDENTIFIER") + System.getenv("COMPUTERNAME");
			byte[] bytes = main.getBytes("UTF-8");
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] md5 = md.digest(bytes);
			int i = 0;
			for (byte b : md5) {
				sb.append(Integer.toHexString((b & 0xFF) | 0x300), 0, 3);
				if (i != md5.length - 1) {
					sb.append("-");
				}
				i++;
			}
			
			System.out.println("HWID: " + sb.toString());
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<String> getUserInfo() {

		List<String> userInfoList = Lists.newArrayList();

		try {
			download(Verify.hwidUrl, Verify.hwidPath);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		File infoFile = new File(Verify.hwidPath);
		if (!infoFile.exists() || !infoFile.isFile()) {
			return null;
		}

		InputStreamReader read = null;
		try {
			read = new InputStreamReader(new FileInputStream(infoFile));

			BufferedReader bufferedReader = new BufferedReader(read);
			String lineTxt = null;
			while ((lineTxt = bufferedReader.readLine()) != null) {

				userInfoList.add(lineTxt);

			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				read.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return userInfoList;
	}

	public static void download(String url, String targetPath) throws UnsupportedEncodingException, IOException {

		Connection connection = Jsoup.connect(url);
		Connection.Response response = connection.method(Connection.Method.GET).ignoreContentType(true)
				.timeout(3 * 1000).execute();
		BufferedInputStream bufferedInputStream = response.bodyStream();
		saveFile(bufferedInputStream, targetPath);
	}

	public static void saveFile(BufferedInputStream bufferedInputStream, String savePath) throws IOException {

		byte[] buffer = new byte[1024];
		int readLenghth;

		FileOutputStream fileOutputStream = new FileOutputStream(new File(savePath));
		BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

		while ((readLenghth = bufferedInputStream.read(buffer, 0, 1024)) != -1) {
			bufferedOutputStream.write(buffer, 0, readLenghth);
		}

		bufferedOutputStream.close();
		fileOutputStream.close();
		bufferedInputStream.close();
	}

	// Call when minecraft client is shutting down by EventAPI
	public static void deleteUserInfo() {
		File file = new File(Verify.hwidPath);
		if (file.exists() && file.isFile()) {
			file.delete();
		}
	}
}
